package vn.vfriends.soffice.bean;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import org.omnifaces.util.Faces;
import org.primefaces.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vn.vfriends.soffice.entity.Menu;
import vn.vfriends.soffice.entity.Permission;
import vn.vfriends.soffice.service.MenuService;
import vn.vfriends.soffice.service.PermissionService;

/**
 *
 * @author chaungoctuan@gmail.com
 */
@ManagedBean
@ViewScoped
public class PermissionBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger(PermissionBean.class);
    
    @Inject private PermissionService permissionService;
    
    @Inject private MenuService menuService;
    
    private Permission[] selectedPermissions;
    private Permission permission;
    
    private Integer permissionId;

    public Permission[] getSelectedPermissions() {
        return selectedPermissions;
    }

    public void setSelectedPermissions(Permission[] selectedPermissions) {
        this.selectedPermissions = selectedPermissions;
    }

    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }

    public Integer getPermissionId() {
        return permissionId;
    }

    public void setPermissionId(Integer permissionId) {
        this.permissionId = permissionId;
    }

    public List<Permission> getPermissionList() {
        return this.permissionService.findAll();
    }
    
    public Permission.Code[] getPermissionCodes() {
        return Permission.Code.values();
    }
    
    public List<Menu> getMenus() {
        return menuService.findAll();
    }
    
    public void preEditAction() {
        try {
            if(this.permission != null){
                Map<String,List<String>> params = new HashMap<String, List<String>>();
                params.put("permissionId", Arrays.asList(this.permission.getId().toString()));
                RequestContext.getCurrentInstance().openDialog("edit", null, params);
            } else if (this.selectedPermissions == null || this.selectedPermissions.length == 0) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một quyền");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else if (this.selectedPermissions.length > 1) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Chỉ chọn một quyền");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            } else {
                this.permission = this.selectedPermissions[0];
                Map<String,List<String>> params = new HashMap<String, List<String>>();
                params.put("permissionId", Arrays.asList(this.permission.getId().toString()));
                RequestContext.getCurrentInstance().openDialog("edit", null, params);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preRenderEditView() {
        this.permission = new Permission();
        this.permission = this.permissionService.find(permissionId);
        if(this.permission == null) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Quyền không tồn tại");
            Faces.getContext().addMessage(null, message);
        }
    }
    
    public void updateAction() {
        try {
            this.permissionService.edit(this.permission);
            this.reset();
            RequestContext.getCurrentInstance().closeDialog("editDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preNewAction() {
        RequestContext.getCurrentInstance().openDialog("new");
    }
    
    public void preRenderNewView() {
        this.permission = new Permission();
    }
    
    public void saveAction() {
        try {
            this.permissionService.create(this.permission);
            this.reset();
            RequestContext.getCurrentInstance().closeDialog("newDialog");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void preDeleteAction() {
        try {
            if ((this.permission == null) && (this.selectedPermissions == null || this.selectedPermissions.length == 0)) {
                RequestContext.getCurrentInstance().addCallbackParam("invalid", true);
                
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Thông báo", "Hãy chọn một hoặc nhiều nhóm");
                RequestContext.getCurrentInstance().showMessageInDialog(message);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    public void deleteAction() {
        try {
            if(this.permission != null) {
                this.permissionService.remove(permission);
            }
            else {
                for(Permission selectedPermission : selectedPermissions) {
                    this.permissionService.remove(selectedPermission);
                }
            }
            this.reset();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Thông báo", "Lỗi hệ thống");
            RequestContext.getCurrentInstance().showMessageInDialog(message);
        }
    }
    
    private void reset() {
        this.permission = null;
        this.permissionId = 0;
        this.selectedPermissions = null;
    }
}
